function e(e) {
  return e && e.__esModule ? e : {
    default: e
  };
}

var t = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var a = arguments[t];
      for (var s in a) Object.prototype.hasOwnProperty.call(a, s) && (e[s] = a[s]);
    }
    return e;
  },
  a = e(require("../../api/api.js")),
  s = e(require("../../utils/util.js")),
  i = e(require("../../utils/filter.js")),
  r = (require("../../config/constants.js"),
    require("../../data/translate.js")),
  config = require('../../config'),
  n = getApp();

Page({
  data: {
    coursesData: [],
    course_id: "",
    courseTitleArr: [],
    courseTitle: "请选择课时",
    courseTitle_current: "",
    courseLengthArr: [{
      course_price_id: 0,
      length: "",
      length_desc: "请选择课时长度"
    }],
    courseLength: "",
    courseLengthDesc: "",
    course_price_id: "",
    availableTimeDatum: [],
    activeDayAvailableTime: [],
    sessionRequestData: {},
    bookSectionTime: "",
    minTimeUnit: 15,
    activeHourIndex: "",
    activeDayIndex: 0,
    wechatPay: !0,
    modalParam: {},
    selected_coupon_cny: "",
    selected_coupon_usd: "",
    canlMove: ""
  },
  //zl：加载选课页面相关数据：课程，可用时间
  onLoad: function(e) {
    console.log(e.title);
    this.setData({
      courseTitle_current: e.title
    });
    var that = this
    wx.request({
      url: config.service.courseUrl,
      data: {
        cater_id: e.id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        //对象取第一个，然后转数组：
        console.log(Object.values(res.data[0]))
        that.setData({
          courseTitle: Object.values(res.data[0])
        });
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
    this.getTeacherAvailableTime(4549902)
  },

  onShow: function() {
    n.globalData.selectedCoupon ? this.useCouponOperate(n.globalData.selectedCoupon) : this.unUseCouponOperate();
  },
  onUnload: function() {
    n.globalData.selectedCoupon = "";
  },

  sessionDayClick: function(e) {
    this.setData({
      activeDayAvailableTime: e.currentTarget.dataset.activeDayAvailableTime,
      activeDayIndex: Number(e.currentTarget.id),
      activeHourIndex: ""
    });
  },
  fetchCourseListByTeacherId: function(e) {
    var t = this;
    a.default.getCourseListByTeacherId({
      query: {
        id: e
      },
      success: function(e) {
        if (e && e.data.data) {
          var a = e.data.data;
          a.map(function(e) {
            e.language_text = i.default.convertSymbolToText(e.language, r.LANGUAGE_DATUM) || e.language,
              e.session_price_cny = i.default.convertCurrencyToCNY(e.session_price_usd, n.globalData.currencyDatum),
              e.trial_price_cny = i.default.convertCurrencyToCNY(e.trial_price_usd, n.globalData.currencyDatum);
          }), t.data.coursesData = a, t.setData({
            coursesData: t.data.coursesData
          });
        }
        e && e.data.meta && (t.mergeTrialCourse(e.data.meta), t.setData({
          coursesData: t.data.coursesData
        })), t.data.coursesData.map(function(e) {
          if (void 0 !== e.title)
            if (e.title) t.data.courseTitleArr.push({
              id: e.id,
              title: e.title
            });
            else {
              var a = "非正式辅导:" + e.language_text;
              t.data.courseTitleArr.push({
                id: e.id,
                title: a
              });
            }
        }), t.setData({
          courseTitleArr: t.data.courseTitleArr
        }), t.bindCourseTitleById();
      }
    });
  },
  bindCourseTitleById: function() {
    if (this.data.course_id) {
      this.initCourseInfoByCourseId(this.data.course_id);
      var e = this.data.courseTitleArr.find(this.isCourseTitle).title;
      this.setData({
        courseTitle: e
      });
    }
  },
  isCourseTitle: function(e) {
    return e.id === this.data.course_id;
  },
  mergeTrialCourse: function(e) {
    if (1 == e.has_trial && !e.has_had_trial && e.trial_left_count > 0) {
      var t = {
        _type: 0,
        has_had_trial: e.has_had_trial,
        name: "30分钟试用课时",
        has_scheduled: e.has_scheduled,
        trial_count: e.trial_count,
        trial_left_count: e.trial_left_count,
        trial_price_usd: e.trial_price_usd
      };
      this.data.coursesData.unshift(t);
    }
  },
  getSessionLengthByCourseId: function(e) {
    var t = this,
      a = this;
    e && a.data.coursesData.map(function(a) {
      a.id === e && a.price_obj_s.map(function(e) {
        var a = e.session_length * t.data.minTimeUnit;
        if (t.data.courseLengthArr.push({
            course_price_id: e.id,
            length: a,
            length_desc: a + "分钟",
            session_type: "session"
          }), e.package_length > 1) {
          var s = e.package_length + "x" + e.session_length * t.data.minTimeUnit;
          t.data.courseLengthArr.push({
            course_price_id: e.id,
            length: s,
            length_desc: s + "分钟",
            session_type: "package"
          });
        }
      });
    }), this.setData({
      courseLengthArr: this.data.courseLengthArr
    });
  },
  getSessionRequest: function(e) {
    var t = this,
      s = wx.getStorageSync("token") || "",
      r = {
        course_price_id: e,
        i_token: s
      };
    a.default.sessionRequest({
      data: r,
      token: s,
      success: function(e) {
        if (e && e.data.data) {
          var a = e.data.data;
          a.available_dollar = i.default.convertCurrencyToUSD(a.available_itc_usd, n.globalData.currencyDatum),
            a.available_cny = i.default.convertCurrencyToCNY(a.available_itc_usd, n.globalData.currencyDatum),
            a.session_cny = i.default.convertCurrencyToCNY(a.session_itc_usd, n.globalData.currencyDatum),
            t.checkPaymentAmount(a), t.setData({
              sessionRequestData: a
            });
        }
      }
    });
  },
  getTrialRequest: function(e) {
    var t = this,
      s = wx.getStorageSync("token") || "",
      r = {
        teacher_id: this.teacherId,
        language: e,
        i_token: s
      };
    a.default.sessionTrialRequest({
      data: r,
      token: s,
      success: function(e) {
        if (e && e.data.data) {
          var a = e.data.data;
          a.available_dollar = i.default.convertCurrencyToUSD(a.available_itc_usd, n.globalData.currencyDatum),
            a.available_cny = i.default.convertCurrencyToCNY(a.available_itc_usd, n.globalData.currencyDatum),
            a.session_cny = i.default.convertCurrencyToCNY(a.session_itc_usd, n.globalData.currencyDatum),
            t.checkPaymentAmount(a), t.setData({
              sessionRequestData: a
            });
        }
      }
    });
  },
  getPackageRequest: function(e) {
    var t = this,
      s = wx.getStorageSync("token") || "",
      r = {
        course_price_id: e,
        i_token: s
      };
    a.default.sessionPackageRequest({
      data: r,
      token: s,
      success: function(e) {
        if (e && e.data.data) {
          var a = e.data.data;
          a.available_dollar = i.default.convertCurrencyToUSD(a.available_itc_usd, n.globalData.currencyDatum),
            a.available_cny = i.default.convertCurrencyToCNY(a.available_itc_usd, n.globalData.currencyDatum),
            a.session_cny = i.default.convertCurrencyToCNY(a.session_itc_usd, n.globalData.currencyDatum),
            t.checkPaymentAmount(a), t.setData({
              sessionRequestData: a
            });
        }
      }
    });
  },
  getTeacherAvailableTime: function(e) {
    var t = this,
      i = wx.getStorageSync("token") || "",
      r = s.default.getDateStr(1) + " 00:00",
      n = s.default.getDateStr(4) + " 23:59",
      c = {
        start_time: r,
        end_time: n,
        i_token: i
      };
    this.page = 1, this.currentDate = {
      start_time: r,
      end_time: n
    }, a.default.getTeacherAvailableTime({
      query: {
        id: e
      },
      data: c,
      success: function(e) {
        if (e && e.data.data) {
          var a = e.data.data.slice(0, 4);
          a.map(function(e) {
            e.calendar_day = s.default.formatCurrentDate(e.day).substring(5), e.week_day = s.default.getWeekByDate(e.day);
          }), t.setData({
            availableTimeDatum: a,
            activeDayAvailableTime: a[0]
          });
        }
      }
    });
  },
  checkPaymentAmount: function(e) {
    var t;
    !this.data.wechatPay && e.available_itc_usd < e.session_itc_usd ? t = e.session_itc_usd - e.available_itc_usd - 100 * this.data.selected_coupon_usd : e.session_itc_usd - 100 * this.data.selected_coupon_usd <= 0 ? (t = 0,
      this.setData({
        wechatPay: !1,
        switch_is_checked: !0
      })) : t = e.session_itc_usd - 100 * this.data.selected_coupon_usd;
    var a = i.default.convertCurrencyToCNY(t, n.globalData.currencyDatum);
    this.setData({
      payment_itc_cny: a
    });
  },
  sessionCheckout: function(e) {
    var t = e.currentTarget.dataset.sessionType,
      a = e.currentTarget.dataset.sessionPrice;
    if (this.data.wechatPay && 0 != this.data.payment_itc_cny) this.wechatPayment(t, a - 100 * this.data.selected_coupon_usd);
    else if (!this.data.wechatPay && this.data.sessionRequestData.available_itc_usd < this.data.sessionRequestData.session_itc_usd) {
      this.data.sessionRequestData.session_itc_usd, this.data.sessionRequestData.available_itc_usd;
      this.wechatPayment(t, a - 100 * this.data.selected_coupon_usd);
    } else this.balancePayment(t);
  },
  balancePayment: function(e) {
    var t = this,
      s = wx.getStorageSync("token") || "",
      i = {
        im_type: 1,
        pwd: this.data.password,
        time: this.data.bookSectionTime,
        course_price_id: this.data.course_price_id,
        student_im_account: "",
        voucher_id: this.data.selected_coupon_id
      },
      r = {
        teacher_id: this.teacherId,
        language: "english",
        im_type: "Z",
        pwd: this.data.password,
        time: this.data.bookSectionTime,
        student_im_account: "",
        voucher_id: this.data.selected_coupon_id
      };
    if ("package" === e) var n = a.default.sessionPackageRequest,
      c = i;
    else if ("trial" === e) var n = a.default.sessionTrialRequest,
      c = r;
    else var n = a.default.sessionRequest,
      c = i;
    n({
      method: "POST",
      data: c,
      token: s,
      success: function(a) {
        if (a && a.data.data) {
          var s = a.data.data;
          if (void 0 !== a.data.data[0] && "teacher time not available" === a.data.data[0].msg || void 0 !== a.data.data[0] && "not in teacher's schedule times" === a.data.data[0].msg) {
            c = "教师时间不可用";
            t.setData({
              sessionErrorInfo: c
            });
          } else if (void 0 !== a.data.data[0] && "student time not available" === a.data.data[0].msg) {
            c = "学生时间不可用";
            t.setData({
              sessionErrorInfo: c
            });
          }
          if (1 == s.length && 1 != s[0].status) console.log("订课失败");
          else {
            var i = s;
            if ("package" === e) {
              var r = i.pid;
              t.bindToSessionResult(r, "package");
            } else if ("trial" === e) {
              n = i.session_id;
              t.bindToSessionResult(n, "trial");
            } else {
              var n = i[0].session_id;
              t.bindToSessionResult(n, "session");
            }
          }
        } else {
          if ("Missing argument pwd" === a.data.error.type) c = "请输入正确的密码";
          else if ("AllSessionFail" === a.data.error.type) c = "课时预定失败，请选择其他时间";
          else if ("need one param time" === a.data.error.msg || "Missing argument time" === a.data.error.type) c = "请选择上课时间";
          else var c = a.data.error.msg || a.data.error.type;
          t.setData({
            sessionErrorInfo: c
          });
        }
      }
    });
  },
  bindToSessionResult: function(e, t) {
    wx.navigateTo({
      url: "../sessions/result/result?id=" + e + "&type=" + t
    });
  },
  wechatPayment: function(e, t) {
    var s = this,
      i = wx.getStorageSync("token") || "",
      r = {
        im_type: 1,
        is_recharge: 1,
        min_price: 2300,
        course_price_id: this.data.course_price_id,
        time: this.data.bookSectionTime,
        student_im_account: ""
      },
      n = {
        im_type: "Z",
        is_recharge: 1,
        min_price: t,
        teacher_id: this.teacherId,
        language: "english",
        time: this.data.bookSectionTime
      };
    if ("package" === e) var c = a.default.sessionPackageRequest,
      o = r;
    else if ("trial" === e) var c = a.default.sessionTrialRequest,
      o = n;
    else var c = a.default.sessionRequest,
      o = r;
    c({
      method: "POST",
      data: o,
      token: i,
      success: function(e) {
        if (e && e.data.data) {
          var r = e.data.data.unpay_id,
            n = {
              pay_type: "X",
              source_type: 5,
              webchat_type: 6,
              itc_usd: t,
              unpay_id: r
            };
          n.min_price = e.data.data.min_price, a.default.wechatPayment({
            method: "POST",
            data: n,
            token: i,
            success: function(e) {
              if (e && e.data.data) {
                if ("user has not bundle wechat" === e.data.data.msg) {
                  i = "账号未绑定微信";
                  s.setData({
                    sessionErrorInfo: i
                  });
                }
                var t = e.data.data;
                wx.requestPayment({
                  timeStamp: t.timeStamp,
                  nonceStr: t.nonceStr,
                  package: t.package,
                  signType: "MD5",
                  paySign: t.paySign,
                  success: function(e) {
                    a.default.unpayCheck({
                      unpay_id: r,
                      success: function(e) {
                        if (e && e.data.data) {
                          var t = e.data.data,
                            a = t.id_list,
                            s = t.session_type,
                            i = t.pay_status,
                            r = "2" === s ? "package" : "session";
                          1 === i && wx.redirectTo({
                            url: "../sessions/detail/detail?id=" + a[0] + "&type=" + r
                          });
                        }
                      }
                    });
                  },
                  fail: function(e) {
                    console.log(e);
                  }
                });
              } else {
                if ("Do not find openid" === e.data.error.msg) i = "账号未绑定微信";
                else var i = e.data.error.msg;
                s.setData({
                  sessionErrorInfo: i
                });
              }
            }
          });
        } else {
          if ("Do not find openid" === e.data.error.msg) c = "账号未绑定微信";
          else if ("Missing argument time" === e.data.error.type || "need one param time" === e.data.error.msg) c = "请选择上课时间";
          else var c = e.data.error.msg;
          s.setData({
            sessionErrorInfo: c
          });
        }
      }
    });
  },
  initCourseInfoByCourseId: function(e) {
    if (this.getSessionLengthByCourseId(e), 1 === e) this.getTrialRequest("english"),
      this.setData({
        selected_session_type: "trial"
      });
    else if (this.data.courseLengthArr.length > 1) {
      var t = this.data.courseLengthArr[1].course_price_id;
      this.data.course_price_id = t, this.getSessionRequest(t), this.setData({
        selected_session_type: "session",
        courseLength: this.data.courseLengthArr[1].length,
        courseLengthDesc: this.data.courseLengthArr[1].length + "分钟"
      });
    }
  },
  switchPaymentChange: function(e) {
    this.setData({
      wechatPay: !e.detail.value
    }), this.checkPaymentAmount(this.data.sessionRequestData);
  },
  switchUseCoupon: function(e) {
    wx.navigateTo({
      url: "/pages/wallet/couponlist/coupon?itc=" + this.data.sessionRequestData.session_itc_usd + "&use_type=1&session_type=1"
    });
  },
  bindPasswordInput: function(e) {
    this.setData({
      password: e.detail.value
    });
  },
  bindCourseTitleChange: function(e) {
    var t = this,
      a = t.data.courseTitleArr[e.detail.value].title,
      s = [{
        course_price_id: 0,
        length: "",
        length_desc: "请选择课时长度"
      }];
    t.setData({
      courseTitle: a,
      courseLengthArr: s
    });
    var i = t.data.courseTitleArr[e.detail.value].id;
    1 === i && (this.setData({
      courseLength: 30,
      courseLengthDesc: "30分钟"
    }), t.getTrialRequest("english")), t.initCourseInfoByCourseId(i);
  },
  bindCourseLengthChange: function(e) {
    var t = this,
      a = t.data.courseLengthArr[e.detail.value].length;
    if ("0" === e.detail.value) s = "请选择课时长度";
    else var s = t.data.courseLengthArr[e.detail.value].length + "分钟";
    this.setData({
      courseLength: a,
      courseLengthDesc: s
    });
    var i = t.data.courseLengthArr[e.detail.value].course_price_id;
    t.data.course_price_id = i, 0 !== i && ("package" === t.data.courseLengthArr[e.detail.value].session_type ? (t.getPackageRequest(i),
      this.setData({
        selected_session_type: "package"
      })) : "trial" === t.data.courseLengthArr[e.detail.value].session_type ? (t.getTrialRequest("english"),
      this.setData({
        selected_session_type: "trial"
      })) : (t.getSessionRequest(i), this.setData({
      selected_session_type: "session"
    })));
  },
  selectSectionTime: function(e) {
    var t = e.currentTarget.dataset.currentDay,
      a = Number(e.currentTarget.id);
    if (this.data.activeHourIndex === a) this.setData({
      activeHourIndex: "",
      bookSectionTime: ""
    });
    else {
      var s = t + " " + e.currentTarget.dataset.currentHour;
      this.setData({
        activeHourIndex: a,
        bookSectionTime: s
      });
    }
  },
  touchStart: function(e) {
    this.touchDot = e.touches[0].pageX, this.setData({
      canlMove: ""
    });
  },
  touchMove: function(e) {
    this.touchMoveX = e.touches[0].pageX;
  },
  touchEnd: function(e) {
    var i = this;
    if (this.touchMoveX && this.touchMoveX - this.touchDot < -75) {
      this.page = this.page + 1;
      var r = s.default.getDateStr(4 * (this.page - 1)) + " 00:00",
        n = s.default.getDateStr(4 * this.page) + " 23:59";
      this.currentDate = {
        start_time: r,
        end_time: n
      };
      var c = this.currentDate;
      this.setData(t({}, this.data, {
        activeDayAvailableTime: [],
        canlMove: "canlMoveLeft"
      })), a.default.getTeacherAvailableTime({
        query: {
          id: this.teacherId
        },
        data: c,
        success: function(e) {
          if (e && e.data.data) {
            var a = e.data.data.slice(0, 4);
            a.map(function(e) {
              e.calendar_day = s.default.formatCurrentDate(e.day).substring(5), e.week_day = s.default.getWeekByDate(e.day);
            }), i.setData(t({}, i.data, {
              availableTimeDatum: a,
              activeHourIndex: "",
              activeDayIndex: 0,
              activeDayAvailableTime: a[0]
            }));
          }
        }
      });
    }
    if (this.page > 1 && this.touchMoveX - this.touchDot > 75) {
      this.page = this.page - 1;
      var o = s.default.getDateStr(4 * (this.page - 1) || 1) + " 00:00",
        u = s.default.getDateStr(4 * this.page) + " 23:59";
      this.currentDate = {
        start_time: o,
        end_time: u
      };
      var d = this.currentDate;
      this.setData(t({}, this.data, {
        activeDayAvailableTime: [],
        canlMove: "canlMoveRight"
      })), a.default.getTeacherAvailableTime({
        query: {
          id: this.teacherId
        },
        data: d,
        success: function(e) {
          if (e && e.data.data) {
            var a = e.data.data.slice(0, 4);
            a.map(function(e) {
              e.calendar_day = s.default.formatCurrentDate(e.day).substring(5), e.week_day = s.default.getWeekByDate(e.day);
            }), i.setData(t({}, i.data, {
              availableTimeDatum: a,
              activeHourIndex: "",
              activeDayIndex: 0,
              activeDayAvailableTime: a[0]
            }));
          }
        }
      });
    }
    this.touchDot = 0, this.touchMoveX = 0;
  },
  useCouponOperate: function(e) {
    0 !== Object.keys(e).length && this.setData({
      selected_coupon_cny: e.coupon_value_cny,
      selected_coupon_usd: e.coupon_value_usd,
      selected_coupon_id: e.voucher_sn
    }), this.checkPaymentAmount(this.data.sessionRequestData);
  },
  unUseCouponOperate: function() {
    this.setData({
      selected_coupon_cny: "",
      selected_coupon_usd: ""
    }), this.checkPaymentAmount(this.data.sessionRequestData);
  },
  leftArrow: function() {
    var e = this;
    this.setData({
      canlMove: "canlMoveLeft"
    }), this.page = this.page + 1;
    var i = s.default.getDateStr(4 * (this.page - 1)) + " 00:00",
      r = s.default.getDateStr(4 * this.page) + " 23:59";
    this.currentDate = {
      start_time: i,
      end_time: r
    };
    var n = this.currentDate;
    this.setData(t({}, this.data, {
      activeDayAvailableTime: [],
      canlMove: "canlMoveLeft"
    })), a.default.getTeacherAvailableTime({
      query: {
        id: this.teacherId
      },
      data: n,
      success: function(a) {
        if (a && a.data.data) {
          var i = a.data.data.slice(0, 4);
          i.map(function(e) {
            e.calendar_day = s.default.formatCurrentDate(e.day).substring(5), e.week_day = s.default.getWeekByDate(e.day);
          }), e.setData(t({}, e.data, {
            availableTimeDatum: i,
            activeHourIndex: "",
            activeDayIndex: 0,
            activeDayAvailableTime: i[0]
          }));
        }
      }
    });
  },
  rightArrow: function() {
    var e = this;
    this.setData({
      canlMove: "canlMoveRight"
    }), this.page = this.page - 1;
    var i = s.default.getDateStr(4 * (this.page - 1) || 1) + " 00:00",
      r = s.default.getDateStr(4 * this.page) + " 23:59";
    this.currentDate = {
      start_time: i,
      end_time: r
    };
    var n = this.currentDate;
    this.setData(t({}, this.data, {
      activeDayAvailableTime: [],
      canlMove: "canlMoveRight"
    })), a.default.getTeacherAvailableTime({
      query: {
        id: this.teacherId
      },
      data: n,
      success: function(a) {
        if (a && a.data.data) {
          var i = a.data.data.slice(0, 4);
          i.map(function(e) {
            e.calendar_day = s.default.formatCurrentDate(e.day).substring(5), e.week_day = s.default.getWeekByDate(e.day);
          }), e.setData(t({}, e.data, {
            availableTimeDatum: i,
            activeHourIndex: "",
            activeDayIndex: 0,
            activeDayAvailableTime: i[0]
          }));
        }
      }
    });
  }
});